package com.wz.jiangsu.controller;


import com.wz.jiangsu.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author syl
 * @description 线程池异步测试
 * @since 2022/7/13
 */
@RestController
@RequestMapping("async")
public class AsyncController {

    @Autowired
    private AsyncService asyncService;

    @GetMapping("test1")
    public void test1(){
        asyncService.executeAsync();
    }


    @GetMapping("test2")
    public String test2() throws ExecutionException, InterruptedException {
        Future<String> future = asyncService.executeAsync1();
        String s;
        //获取到执行结果才返回
        while (true) {
            if (future.isDone()) {
                s=future.get();
                break;
            }
        }
        return s;
    }
}
